package com.xm;

import java.io.*;
import java.net.URISyntaxException;
import java.time.LocalDateTime;

public class CompressAndDelete {
    private static final String LOG_FILE;

    static  {
        String jarPath;
        try {
            jarPath = CompressAndDelete.class.getProtectionDomain()
                    .getCodeSource().getLocation().toURI().getPath();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
        String jarDir = new File(jarPath).getParent();
        LOG_FILE = jarDir + File.separator + "CompressAndDelete.log";
    }
    public CompressAndDelete() {
    }

    public static void main(String[] var0) {
        try {
            log("程序启动，参数: " + String.join(" ", var0));
            if (var0.length == 0) {
                throw new IllegalArgumentException("请拖放文件或文件夹到本程序上使用");
            }

            String var1 = var0[0];
            File var2 = new File(var1);
            if (!var2.exists()) {
                throw new FileNotFoundException("文件不存在: " + var1);
            }

            String var3 = findWinRARPath();
            if (var3 == null) {
                throw new IllegalStateException("未找到WinRAR安装路径");
            }

            String var4 = var1 + ".zip";
            String var5 = String.format("\"%s\" a -afzip -df -r -ep1 \"%s\" \"%s\"", var3, var4, var1);
            log("执行命令: " + var5);
            Process var6 = Runtime.getRuntime().exec(var5);
            int var7 = var6.waitFor();
            if (var7 != 0) {
                throw new RuntimeException("压缩失败，WinRAR返回码: " + var7);
            }

            log("压缩完成，已删除源文件");
            System.exit(0);
        } catch (Exception var9) {
            logError("错误: " + var9.getMessage());
            var9.printStackTrace();
            System.err.println("按Enter键退出...");

            try {
                System.in.read();
            } catch (IOException var8) {
            }

            System.exit(1);
        }

    }

    private static String findWinRARPath() {
        String[] var0 = new String[]{"C:\\Program Files\\WinRAR\\WinRAR.exe", "C:\\Program Files (x86)\\WinRAR\\WinRAR.exe"};
        String[] var1 = var0;
        int var2 = var0.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            String var4 = var1[var3];
            if ((new File(var4)).exists()) {
                log("找到WinRAR路径: " + var4);
                return var4;
            }
        }

        return null;
    }

    private static void log(String var0) {
        try {
            PrintWriter var1 = new PrintWriter(new FileWriter(LOG_FILE, true));

            try {
                LocalDateTime localDateTime = LocalDateTime.now();
                var1.println(localDateTime + " [INFO] " + var0);
            } catch (Throwable var5) {
                try {
                    var1.close();
                } catch (Throwable var4) {
                    var5.addSuppressed(var4);
                }

                throw var5;
            }

            var1.close();
        } catch (IOException var6) {
            System.err.println("无法写入日志: " + var6.getMessage());
        }

        System.out.println(var0);
    }

    private static void logError(String message) {
        try {
            PrintWriter var1 = new PrintWriter(new FileWriter(LOG_FILE, true));

            try {
                LocalDateTime dateTime = LocalDateTime.now();
                var1.println(dateTime + " [ERROR] " + message);
            } catch (Throwable e) {
                try {
                    var1.close();
                } catch (Throwable var4) {
                    e.addSuppressed(var4);
                }
                throw e;
            }

            var1.close();
        } catch (IOException var6) {
            System.err.println("无法写入错误日志: " + var6.getMessage());
        }

        System.err.println(message);
    }
}